import sensor
import image
import time

# 初始化摄像头
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)

# 设置颜色阈值
red_threshold = (30, 100, 15, 127, 15, 127)   # 红色阈值范围，可根据需要调整
yellow_threshold = (60, 100, -128, -10, -128, 0)  # 黄色阈值范围，可根据需要调整

while True:
    # 捕获图像
    img = sensor.snapshot()

    # 查找矩形
    for r in img.find_rects(threshold=10000):
        # 检查矩形颜色
        stats = img.get_statistics(roi=r)
        if stats.l_mean() > stats.b_mean() and stats.b_mean() > stats.g_mean():
            # 检查红色阈值
            red_blobs = img.find_blobs([red_threshold], roi=r, pixels_threshold=200, area_threshold=200)
            if red_blobs:
                # 在红色矩形内查找黄色圆形
                for red_blob in red_blobs:
                    yellow_blobs = img.find_blobs([yellow_threshold], roi=red_blob.rect(), pixels_threshold=200, area_threshold=200)
                    if yellow_blobs:
                        for yellow_blob in yellow_blobs:
                            # 绘制识别结果
                            img.draw_circle(yellow_blob.x(), yellow_blob.y(), yellow_blob.r())
                            img.draw_rectangle(yellow_blob.rect())
                            img.draw_cross(yellow_blob.cx(), yellow_blob.cy())

